
import de.uniba.wiai.lspi.chord.service.ServiceException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.security.InvalidKeyException;
import java.security.SignatureException;
import java.util.logging.Level;
import java.util.logging.Logger;
import routing.OpenChordWrapper;
import routing.UsernameKey;
import routing.exception.BadPortException;
import routing.exception.BadSignatureException;
import routing.exception.UserExistException;
import routing.exception.UserNotFoundException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Paweł
 */
public class TEST {
    
    private static OpenChordWrapper chord;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws UnknownHostException, MalformedURLException, ServiceException, UserExistException, UserNotFoundException {
        createOrConnect();

    }

    private static void createOrConnect() throws UnknownHostException, MalformedURLException, ServiceException, UserNotFoundException {
        chord = new OpenChordWrapper();
        try {
            chord.create(8080);
        } catch (BadPortException ex) {
            // TODO trzeba lapac ten blad
            chord = new OpenChordWrapper();
            chord.join(8282, "192.168.1.104", 8080);
            try {
                try {
                    //insertData();
                    chord.changeState("pawel", false);
                } catch (UserExistException ex1) {
                    throw new RuntimeException(ex1);
                }
                try {
                    chord.leave("pawel");
                } catch (UserExistException ex1) {
                    throw new RuntimeException(ex1);
                } catch (Exception ex2) {
                    throw new RuntimeException(ex2);
                }
            chord = new OpenChordWrapper();
            chord.join(8081, "192.168.1.104", 8080);
                try {
                    chord.removeUser("pawel");
                } catch (BadSignatureException ex1) {
                    new RuntimeException(ex1);
                }
            } catch (InvalidKeyException ex1) {
                Logger.getLogger(TEST.class.getName()).log(Level.SEVERE, null, ex1);
            } //catch (BadSignatureException ex1) {
//                throw new RuntimeException(ex1);
//            }
//            System.out.println(chord.getUserData("pawel").toString());
//            System.out.println(chord.getUserData("andrzej").toString());
        }
    }

    private static void insertData() {
        try {
            try {
                chord.insertUser("pawel", "password");
            } catch (UserExistException ex) {
                throw new RuntimeException(ex);
            }
        } catch (InvalidKeyException ex) {
            throw new RuntimeException(ex);
        } catch (SignatureException ex) {
            throw new RuntimeException(ex);
        } catch (ServiceException ex) {
            throw new RuntimeException(ex);
        }
    }

    private static void removeData() {
        UsernameKey uk = new UsernameKey("pawel");
        //chord.removeUser(uk, null, null, sign);
        throw new UnsupportedOperationException();
    }

}
